package DataLayer.ImageDataContainer.ImageComponentsInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class CharacterStatistics {

    private int numberOfChar;
    private int occMatrix[][][];
    private String occMatrixPath = "./Occ.om";
    private int maxCharApp;

    /**
     * The constructor.
     */
    public CharacterStatistics(){
        numberOfChar = 26;
        occMatrix = new int [numberOfChar][numberOfChar][numberOfChar];
        loadOccMatrix();
        maxCharApp = getMaxApp();
    }


    /**
     *
     * @return max from the occMatrix
     */
    public int getMaxApp(){
        int i,j,k;
        int max = 0;
        for(i = 0; i < numberOfChar; i++){
            for(j = 0; j < numberOfChar; j++){
                for(k = 0; k < numberOfChar; k++){
                    if (occMatrix[i][j][k] > max){
                        max = occMatrix[i][j][k];
                    }
                }
            }
        }
        return max;
    }

    /**
     * sets the number of characters
     * @param num
     */
    public void setNumberOfChar(int num){
        numberOfChar = num;
    }

    /**
     * @param c
     * @return the index of the character in the matrix
     */
    public int getCharIndex(char c){
        return Character.getNumericValue(c)-10;
    }

    /**
     *@return the occurence of a letter after the two letters
     *@param char3 - actual letter
     *       char1,char2 - the two letters before the actual
     */
    public double getOcc(char letter1, char letter2, char letter3){
        return occMatrix[getCharIndex(letter1)][getCharIndex(letter2)][getCharIndex(letter3)]*1.0 / maxCharApp;
    }

    /**
     * create the occurrence matrix from a text file
     * @param filePath - name of the text file
     */
    public void createOccMatrix(String filePath){
        int charMatrix[] = new int[3];  //for the last 3 letters
        int counter = 0;  //counting the number of letters before the actual letter

        File file = new File(filePath); //input file
        try{
            FileInputStream fis1 = new FileInputStream(file);
            int reader;

            reader = fis1.read();   //read from input
            while(reader != -1){
                if((reader > 64)&&(reader < 91)){
                    reader = reader + 32;
                }
                if ((reader > 96)&&(reader < 123)){
                    charMatrix[2] = charMatrix[1];
                    charMatrix[1] = charMatrix[0];
                    charMatrix[0] = reader - 97;    //step forward,getting the index
                    counter++;
                    if (counter > 2) {
                        occMatrix[charMatrix[2]][charMatrix[1]][charMatrix[0]]++;  //incrementing the occurrence of the letter
                    }
                }else{
                    if(reader != 39){   //if the character is ' then stepping forward in word
                        counter = 0;
                    }
                }
                reader = fis1.read();
            }

        }catch(FileNotFoundException ex){
            System.out.println(ex);
        }catch(IOException ex){
            System.out.println(ex);
        }
    }

    /**
     * write the matrix to the harddisk
     */
    public void exportOccMatrix(){
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(new FileOutputStream(new File(occMatrixPath)));
            out.writeObject(occMatrix);
        } catch (IOException ex) {
            System.out.print(ex);
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                System.out.print(ex);
            }
        }
    }

    /**
     * load the occurrence matrix from the harddisk
     */
    public void loadOccMatrix(){
        ObjectInputStream in = null;
        try{
            in = new ObjectInputStream(new FileInputStream(new File(occMatrixPath)));
            occMatrix = (int [][][]) in.readObject();
        }catch(Exception ex){
            System.out.println(ex);
        }
    }

}
